package com.deke.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deke.entity.AclUser;
import com.deke.security.annotation.RequiresPermission;
import com.deke.system.service.AclUserService;
import com.deke.system.vo.UserVo;
import com.deke.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/12/1 18:48
 * @description：
 * @modified By：
 * @version:
 **/
@Api(tags = "用户接口类")
@RestController
@RequestMapping("/system/user")
public class AclUserController {
    @Autowired
    private AclUserService userService;

    @GetMapping("queryByUsername/{username}")
    @ApiOperation(value = "通过用户名查询用户")
    @ApiImplicitParam(value = "用户名称",name = "username")
    public Result<AclUser> queryByUsername(@PathVariable String username){
        AclUser aclUser = userService.queryByUsername(username);
        if (ObjectUtil.isNotNull(aclUser)){
            Result<AclUser> result = new Result( 2000,"查询成功",aclUser);
            return result;
        }
        return null;
    }
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 获取用户信息 (头部导航栏) 
     * @create time: 2022/12/4 0:32
     * @param null 
     * @return :  
     **/
    @GetMapping("info")
    @ApiOperation(value = "获取用户信息")
    public Result<AclUser> info(){
        return userService.getUserinfo();
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 分页查询 
     * @create time: 2022/12/4 0:48
     * @param null
     * @return :  
     **/
    @PostMapping("/getUserByPage/{current}/{pageSize}")
    @ApiOperation(value = "用户信息模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页码数",name = "current"),
            @ApiImplicitParam(value = "每页的行数",name = "pageSize"),
            @ApiImplicitParam(value = "前端实体类",name = "userVo")
    })
    public Result<IPage<AclUser>> getUserByPage(@PathVariable(required = true) Integer current, @PathVariable(required = true) Integer pageSize, @RequestBody UserVo userVo){
        return  userService.queryByConditionPage(current,pageSize,userVo);
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 添加用户 
     * @create time: 2022/12/4 1:25
     * @param null
     * @return :  
     **/
    @RequiresPermission(value = "user:add")
    @PostMapping("addUserByUser")
    @ApiOperation(value = "添加用户")
    @ApiImplicitParam(value = "前端实体类",name = "userVo")
    public Result addUserByUser(@RequestBody UserVo userVo){
         return userService.addUserByUser(userVo);
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 用户状态更新 
     * @create time: 2022/12/4 18:14
     * @param null
     * @return :  
     **/
    @RequiresPermission(value = "user:update")
    @PostMapping("updateUserStatusByid/{id}/{deleted}")
    @ApiOperation(value = "通过用户ID修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID",name = "id"),
            @ApiImplicitParam(value = "用户状态",name = "deleted")
    })
    public Result updateUserStatusByid(@PathVariable String id, @PathVariable Integer deleted){
        return userService.updateUserStatusByid(id,deleted);
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 编辑 
     * @create time: 2022/12/4 18:52
     * @param null
     * @return :  
     **/
    @RequiresPermission(value = "user:update")
    @PostMapping("updateUserByUser")
    @ApiOperation(value = "修改用户信息")
    @ApiImplicitParam(value = "实体类",name = "aclUser")
    public Result updateUserByUser(@RequestBody AclUser aclUser){
       return userService.updateUserByUser(aclUser);
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 根据id 删除用户 
     * @create time: 2022/12/5 13:44
     * @param null
     * @return :  
     **/
    @RequiresPermission(value = "user:remove")
    @DeleteMapping("/deleteUserById/{id}")
    @ApiOperation(value = "根据用户ID删除用户")
    @ApiImplicitParam(value = "用户id",name = "id")
    public Result deleteUserById (@PathVariable String id){
        Result result = userService.deleteUserById(id);
        System.out.println(result.toString());
        return result;
    }
}